package it.polimi.comorazio.adapters;

import it.polimi.comorazio.R;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class ImageAdapter extends BaseAdapter {

	private Context ctx;
	int imageBackground;
	private ImageCallbacks mCallbackImpl;

	public interface ImageCallbacks {
		public int getNumberOfImages();

		public int getImageIdentifier(int position, Resources resources);
	}

	public ImageAdapter(Context c, ImageCallbacks pCallbackImpl) {
		ctx = c;
		this.mCallbackImpl = pCallbackImpl;
		TypedArray ta = c.obtainStyledAttributes(R.styleable.Gallery1);
		imageBackground = ta.getResourceId(
				R.styleable.Gallery1_android_galleryItemBackground, 1);
		ta.recycle();
	}

	@Override
	public int getCount() {
		return mCallbackImpl.getNumberOfImages();
	}

	@Override
	public Object getItem(int arg0) {
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		ImageView iv = new ImageView(ctx);
		iv.setImageResource(mCallbackImpl.getImageIdentifier(arg0,
				ctx.getResources()));
		iv.setScaleType(ImageView.ScaleType.FIT_XY);
		iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
		iv.setBackgroundResource(imageBackground);
		return iv;
	}

}